<template>
    <div class="mod-prod">
        <avue-crud ref="crud"
                   :data="dataList"
                   :option="tableOption"
                   :page="page"
                   :permission="permission"
                   :table-loading="dataListLoading"
                   @search-change="searchChange"
                   @selection-change="selectionChange"
				    @refresh-change="refreshChange"
                   @on-load="getDataList">
          <!-- @row-del="deleteHandle"-->
            
            <template slot="menuLeft">
                <div style="display: flex">
                    
                    <div style="width: 100%;display: flex;">
                        <el-date-picker type="date" placeholder="起始日期" v-model="beginDate" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                        <div style="width: 50px;display: flex;align-items: center;justify-content: center;">-</div>
                        <el-date-picker type="date" placeholder="结束日期" v-model="endDate" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                        
                        
                        <el-input placeholder="客户名称"  v-model="biz3" label="客户名称" style="margin: 0 20px"></el-input>
                        <el-button icon="el-icon-search"
                                   size="small"
                                   type="primary" @click="getDataList({currentPage: 1,pageSize: 10})" style="margin-left: 20px;">查询
                        </el-button>
                    </div>
                </div>
            </template>
            <template slot="menu"
                      slot-scope="scope">
                
                <el-button
                    icon="el-icon-delete"
                    size="small"
                    type="danger"
                    @click="deleteHandle(scope.row.id)">删除
                </el-button>
            </template>

        </avue-crud>
        <add-or-update v-if="addOrUpdateVisible"
                       ref="addOrUpdate"
                       @refreshDataList="refreshChange"></add-or-update>
    </div>
</template>

<script>
import {tableOption} from '@/crud/customer/levelChangeLogList'
import AddOrUpdate from '@/views/modules/customer/cust-add-update.vue'

export default {
    components: {AddOrUpdate},
	data () {
		return {
			dataForm: {
				prodName: ''
			},
			permission: {
				delBtn: this.isAuth('prod:prod:delete')
			},
			dataList: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
                pageSize: 1000, // 每页显示多少条
                pageSizes: [1000]
			},
            addOrUpdateVisible: false,
			dataListSelections: [],
			dataListLoading: false,
			tableOption: tableOption,
			resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
            beginDate: wfy.format("yyyy-MM" + "-01", new Date()),
            endDate: wfy.format("yyyy-MM-dd", new Date()),
		}
	},
    mounted () {
        this.initData()
    },
    methods: {
        initData(){
            this.$http({
                url: this.$http.adornUrl('/crm/base/emp/page'),
                method: 'get',
                params: this.$http.adornParams(Object.assign({
                    current: 1,
                    size: 999
                }))
            }).then(({data}) => {
                const targetColumn = this.tableOption.column.find((col) => {
                    return col.prop === "erpCustCode"
                });
                let optionList = data.obj.records.map(i => {
                    return {
                        label: i.empName,
                        value: i.id
                    }
                })
                this.$set(targetColumn, 'dicData', optionList);
            })
        },
		// 获取数据列表
		getDataList (page, params, done) {
			this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl('/crm/base/cust/page'),
				method: 'get',
				params: this.$http.adornParams(
					Object.assign(
						{
							current: page == null ? this.page.currentPage : page.currentPage,
							size: page == null ? this.page.pageSize : page.pageSize
						},
						params
					)
				)
			}).then(({data}) => {
				this.dataList = data.obj.records
				this.page.total = data.obj.total
				this.dataListLoading = false
				if (done) {
					done()
				}
			})
		},
        // 新增 / 修改
        addOrUpdateHandle (id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },
		// 删除和批量删除
		deleteHandle (id) {
			console.log('id:',id);
			this.$confirm(`确定进行删除操作?`, '友情提醒', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.$http({
						url: this.$http.adornUrl(`/crm/base/cust/deleteBaseCustInfo`),
						method: 'delete',
						data: this.$http.adornData({id: id}, false)
					}).then(({data}) => {
						this.getDataList(this.page)
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
							}
						})
					})
				})
				.catch(() => {
				})
		},
		// 条件查询
		searchChange (params, done) {
			this.getDataList(this.page, params, done)
		},
		// 多选变化
		selectionChange (val) {
			this.dataListSelections = val
		},
		// 获取选中的商品Id列表
		getSeleProdIds () {
			return this.dataListSelections.map(item => {
				return item.prodId
			})
		},
        /**
         * 刷新回调
         */
        refreshChange () {
            this.getDataList(this.page)
        },
	}
}
</script>
